package com.study.springboot.SpringBoot_A.mq.rabbitmq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 赵超
 * @Description Topic Exchange配置
 * @create 2018-03-29 10:12
 **/
@Configuration
public class TopicRabbitConfig {

    /**
     * queue的名字
     */
    static final String message = "topic.message";
    static final String messages = "topic.messages";

    /**
     * 创建一个名为topic.mess的无关queue实例
     * @return
     */
    @Bean
    public Queue queueMes() {
        return new Queue("topic.mes");
    }

    /**
     * 创建一个名为topic.message的queue实例
     * @return
     */
    @Bean
    public Queue queueMessage() {
        return new Queue(TopicRabbitConfig.message);
    }

    /**
     * 创建一个名为topic.messages的queue实例
     * @return
     */
    @Bean
    public Queue queueMessages() {
        return new Queue(TopicRabbitConfig.messages);
    }

    /**
     * 创建名称为exchange的交换机
     * @return
     */
    @Bean
    TopicExchange exchange() {
        return new TopicExchange("exchange");
    }

    /**
     * 设置路由模式为‘topic.message’，将路由键‘topic.message’绑定到名为‘topic.message’的queue上，
     * 绑定后，名为‘topic.message’的queue只可以收到‘topic.message’发送的消息
     * @param queueMessage
     * @param exchange
     * @return
     */
    @Bean
    Binding bindingExchangeMessage(Queue queueMessage, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
    }

    /**
     * 设置路由模式为‘topic.#’，将路由键‘topic.#’绑定到名为‘topic.messages’的queue上，
     * 绑定后，名为‘topic.messages’的queue可以收到‘topic.任何后缀’发送的消息
     * @Description *表示一个词，#表示零个或多个词.
     * @param queueMessages
     * @param exchange
     * @return
     */
    @Bean
    Binding bindingExchangeMessages(Queue queueMessages, TopicExchange exchange) {
        return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
    }
}
